
package com.wiesloch.controller;
/**
 * <p>ClassName:RepertoryController</p>
 * <p>Description:</p>
 * <p> E-mail:jintao_iknow@163.com </p>
 * Date:     2016年5月17日 上午9:40:26 <br/>
 * @author   tao95
 * @version  1.0
 * @since    JDK 1.7
 *
 */

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.wiesloch.po.Distributor;
import com.wiesloch.po.Goods;
import com.wiesloch.po.GoodsVo;
import com.wiesloch.po.Repertory;
import com.wiesloch.po.RepertoryExtends;
import com.wiesloch.service.GoodsService;
import com.wiesloch.service.RepertoryService;

@Controller
@RequestMapping("repertoryController")
public class RepertoryController {

	@Autowired
	private GoodsService goodsService;
	@Autowired
	private RepertoryService repertoryService;
	
	@RequestMapping(value="/allSeriesUI",method = { RequestMethod.POST, RequestMethod.GET })
	public ModelAndView allSeriesUI(HttpServletRequest request)throws Exception{
		ModelAndView modelAndView = new ModelAndView();
		List<Goods> resultList = goodsService.selectAllSeries();
		List<GoodsVo>seriesList = new ArrayList<GoodsVo>();
		for (Goods good : resultList) {
			
			String pictureUrl = good.getGoods_pictureUrl();
			String [] pics = pictureUrl.split(";");
			GoodsVo goodsVo = new GoodsVo();
			goodsVo.setGoods(good);
			goodsVo.setPictureUrl(pics[0]);
			seriesList.add(goodsVo);
			System.out.println();
		}
		modelAndView.addObject("seriesList", seriesList);
		modelAndView.setViewName("respertory/allSeries");
		return modelAndView;
	}
	
	@RequestMapping(value="/allGoodsUI",method = { RequestMethod.POST, RequestMethod.GET })
	public ModelAndView allGoodsUI(HttpServletRequest request,Integer goods_pid) throws Exception{
		ModelAndView modelAndView = new ModelAndView();
		HttpSession session = request.getSession();
		Distributor distributor = (Distributor) session.getAttribute("distributor");
		Repertory repertory = new Repertory();
		repertory.setRepertory_distributor_id(distributor.getDistributor_id());
		repertory.setRepertory_goods_id(goods_pid);
		List<RepertoryExtends> resultList = repertoryService.selectRepertoryBySeriesIdAndDistributorId(repertory);
		for (RepertoryExtends repertoryExtends : resultList) {
			String pictureUrl = repertoryExtends.getGoods_pictureUrl();
			String [] pics = pictureUrl.split(";");
			repertoryExtends.setPictureUrl(pics[0]);
			System.out.println(repertoryExtends);
		}
		modelAndView.addObject("resultList", resultList);
		modelAndView.setViewName("respertory/allGoods");
		return modelAndView;
	}
	
	@RequestMapping(value="/inOutDistributorGoods",method = { RequestMethod.POST, RequestMethod.GET })
	public ModelAndView inOutDistributorGoods(HttpServletRequest request,RepertoryExtends repertoryInExtends) throws Exception{
		ModelAndView modelAndView = new ModelAndView();
		System.out.println(repertoryInExtends);
		HttpSession session = request.getSession();
		Distributor distributor = (Distributor) session.getAttribute("distributor");
		RepertoryExtends repertoryOutExtends = repertoryService.selectParentRepertory(repertoryInExtends);
		if(repertoryOutExtends.getRepertory_amount() < repertoryInExtends.getAmount()){
			repertoryOutExtends = repertoryService.selectParentRepertory(repertoryInExtends);
		}
		repertoryOutExtends.setAmount(repertoryInExtends.getAmount());
		repertoryService.updateInRepertoryAmount(repertoryInExtends);
		repertoryService.updateOutRepertoryAmount(repertoryOutExtends);
		return modelAndView;
	}
}
